﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Linq;
using DG.Tweening;

namespace HKTools
{
    public class HK_SimpleDialog3D : HK_SimpleDialogController
    {
        [Header("启用打字机效果："), SerializeField] bool isWriterEffectOn = false;

        [Header("对话面板："), SerializeField] Transform dialogPanel_TF;
        HK_DialogPanelCtrl dialogPanelCtrl;

        [Header("选项面板："), SerializeField] Transform optionPanel_TF;
        HK_OptionPanelCtrl optionPanelCtrl;

        [Header("角色头像："), SerializeField] Image head_Img;
        [Header("角色名称："), SerializeField] Text name_Txt;
        [Header("对话文本："), SerializeField] Text dialog_Txt;
        [Header("继续按钮："), SerializeField] Button next_Btn;
        [Header("选项按钮预制体："), SerializeField] Button option_Btn;

        // 角色图片列表
        [Header("头像列表："), SerializeField] List<Sprite> headSprites;
        [Header("对话框位置列表："), SerializeField] List<Transform> dialogTargets;
        [Header("选项框位置："), SerializeField] Transform optionTarget;

        void Awake()
        {
            dialogPanelCtrl = dialogPanel_TF.GetComponent<HK_DialogPanelCtrl>();
            optionPanelCtrl = optionPanel_TF.GetComponent<HK_OptionPanelCtrl>();

            next_Btn.onClick.AddListener(() =>
            {
                this.SendCommand(new CMD_PlayForwardDialog());
            });
        }

        /// <summary>
        /// 开始时，初始化
        /// </summary>
        public void Init()
        {
            gameObject.SetActive(true);

            Debug.Log("Simple Dialog 3D Init...");
        }

        /// <summary>
        /// 结束时，反初始化
        /// </summary>
        public void DeInit()
        {
            dialogPanelCtrl.HideAnim(true);
            optionPanelCtrl.HideAnim(true);
            gameObject.SetActive(false);

            Debug.Log("Simple Dialog 3D DeInit...");
        }

        public void OnDialogUpdate(string name, string dialog)
        {
            Transform dialogTarget = dialogTargets.FirstOrDefault(target => target.name == name);

            // 如果 位置改变了 就进行切换
            if (dialogTarget != null && dialogPanel_TF.position != dialogTarget.position)
            {
                dialogPanelCtrl.ShowAnim();

                dialogPanel_TF.position = dialogTarget.position;
            }

            name_Txt.text = name;

            if (isWriterEffectOn)
            {
                DOTween.Complete(dialog_Txt);

                dialog_Txt.text = "";
                dialog_Txt.DOText(dialog, dialog.Length / 11f).SetEase(Ease.Linear);
            }
            else
            {
                dialog_Txt.text = dialog;
            }

            head_Img.gameObject.SetActive(true);

            Sprite headSprite = headSprites.FirstOrDefault(sprite => sprite.name == name);

            if (headSprite != null)
            {
                head_Img.sprite = headSprite;
            }
            else
            {
                head_Img.sprite = null;
            }

            if (!next_Btn.gameObject.activeInHierarchy)
                next_Btn.gameObject.SetActive(true);
        }

        public void OnShowOptions(List<string> optionText_List, List<int> nextDialogIndex_List)
        {
            dialogPanelCtrl.HideAnim();
            optionPanelCtrl.ShowAnim();

            if (optionTarget != null && optionPanel_TF.position != optionTarget.position)
            {
                optionPanel_TF.position = optionTarget.position;
            }

            if (next_Btn.gameObject.activeInHierarchy)
                next_Btn.gameObject.SetActive(false);

            for (int i = 0; i < nextDialogIndex_List.Count; i++)
            {
                int index = i;

                Button optionBtn = Instantiate(option_Btn, optionPanel_TF);
                optionBtn.GetComponentInChildren<Text>().text = optionText_List[i];
                optionBtn.onClick.AddListener(() =>
                {
                    this.GetSystem<IHK_SimpleDialogSystem>().JumpDialog(nextDialogIndex_List[index], optionText_List[index]);

                    for (int j = 0; j < optionPanel_TF.childCount; j++)
                    {
                        Destroy(optionPanel_TF.GetChild(j).gameObject);
                    }

                    dialogPanelCtrl.ShowAnim();
                    optionPanelCtrl.HideAnim();
                });
            }
        }
    }
}